package cloudoer.su.action;

import cloudoer.su.base.impl.BaseAction;
import cloudoer.su.entity.Menu;
import cloudoer.su.entity.Role;
import cloudoer.su.entity.User;
import com.opensymphony.xwork2.ActionContext;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.*;

@Controller
@Scope("prototype")
public class UserAction extends BaseAction {

    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String indexUI (){
        return "success";
    }

    public String addUI(){
        return "success";
    }

    public String importUI(){
        return "success";
    }

    public String getAll() throws IOException {
        List<User> list = userService.getAll();
        ajaxJson(list,new String[]{"roles","classes","dormitory","classCommittees"});
        return null;
    }

    public String getByPage() throws IOException {
        int pageNo = Integer.parseInt(ServletActionContext.getRequest().getParameter("page"));
        int pageSize = Integer.parseInt(ServletActionContext.getRequest().getParameter("rows"));
        List<User> list = userService.getByPage(pageNo, pageSize);
        ajaxDataGrid(userService.getCount(), list, new String[]{"roles","classes","dormitory","classCommittees"});
        return null;
    }

    public String getById () throws IOException {
        String id = ServletActionContext.getRequest().getParameter("id");
        User user = userService.getById(id);
        ajaxJson(user, new String[]{"roles","classes","dormitory","classCommittees"});
        return null;
    }

    public String getByNumber () throws IOException {
        String number = ServletActionContext.getRequest().getParameter("number");
        User user = userService.getById(number);
        ajaxJson(user, new String[]{"roles","classes","dormitory","classCommittees"});
        return null;
    }

    public String add() throws IOException {
        String personNumber = ServletActionContext.getRequest().getParameter("personNumber");
        String roleNumbers = ServletActionContext.getRequest().getParameter("roleNumbers");
        String password0 = ServletActionContext.getRequest().getParameter("password0");
        if (!user.getPassword().equals(password0)){
            ajaxError("两次密码不一致");
            return null;
        }
        userService.add(user, personNumber,roleNumbers.split("▁"));
        ajaxSuccess("添加成功");
        return null;
    }

    public String update() throws IOException {
        String personNumber = ServletActionContext.getRequest().getParameter("personNumber");
        String roleNumbers = ServletActionContext.getRequest().getParameter("roleNumbers");
        userService.update(user, personNumber,roleNumbers.split("▁"));
        ajaxSuccess("修改成功");
        return null;
    }

    public String delete() throws IOException {
        String id = ServletActionContext.getRequest().getParameter("id");
        userService.delete(id);
        ajaxSuccess("删除成功");
        return null;
    }

    public String importFile() throws IOException {
        Map<String, Object> map = getExcalFile();
        File file = (File) map.get("file");
        String fileName = (String) map.get("name");
        if (file == null){
            ajaxError("文件为空，导入失败");
            return null;
        }
        if (fileName.matches("^.+\\.(?i)((xls)|(xlsx))$")){//检查是否是Excel文件
            String msg = userService.importFile(file);
            ajaxSuccess(msg);
        }else {
            ajaxError("文件格式错误，请按规范提交");
            return null;
        }
        return null;
    }

    public String exportFile() throws Exception {
        OutputStream os = ServletActionContext.getResponse().getOutputStream();
        ServletActionContext.getResponse().setContentType("application/x-execl");
        ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename=" + new String("用户数据.xls".getBytes(), "ISO-8859-1"));
        userService.exportFile(os);
        return null;
    }

    public String getRoles () throws IOException {
        String id = ServletActionContext.getRequest().getParameter("id");
        Set<Role> set = userService.getRoles(id);
        ajaxJson(set, new String[]{"users"});
        return null;
    }

    public String deleteRole() throws IOException {
        String id = ServletActionContext.getRequest().getParameter("id");
        String roleId = ServletActionContext.getRequest().getParameter("roleId");
        userService.deleteRole(id, roleId);
        ajaxSuccess("删除成功");
        return null;
    }

}
